﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using Hotel.Classes.Helper;
using Hotel.Models.Entity;

namespace Hotel.Models.DataAccess
{
    public class DAStaff
    {
        public static List<Staff> GetListStaff(int staffID)
        {
            DataTable dt;
            List<Staff> list = new List<Staff>();
            using (DBManager dbManager = new DBManager("stp_Get_Staff"))
            {
                dbManager.Add("p_PK_StaffID", staffID);
                dbManager.Add("IsDelete", 0);
                dt = dbManager.GetDatatable();
            }
            foreach (DataRow dr in dt.Rows)
            {
                Staff staff = new Staff();
                staff.PkStaffId = DataHelper.GetInt(dr, "PK_StaffID");

                GroupStaff gs = new GroupStaff();
                gs.PkGroupStaffId = DataHelper.GetInt(dr, "FK_GroupStaffID");
                staff.GroupStaff = gs;

                staff.UserName = DataHelper.Get(dr, "UserName");
                staff.PassWord = DataHelper.Get(dr, "Password");
                staff.Name = DataHelper.Get(dr, "Name");
                staff.Address = DataHelper.Get(dr, "Address");
                staff.Phone = DataHelper.Get(dr, "Phone");
                staff.Email = DataHelper.Get(dr, "Email");
                staff.Passport = DataHelper.Get(dr, "Passport");
                staff.BirthDay = DataHelper.GetDate(dr, "BirthDay");
                staff.BirthPlace = DataHelper.Get(dr, "BirthPlace");
                staff.Sojourn = DataHelper.Get(dr, "Sojourn");
                staff.Resident = DataHelper.Get(dr, "Resident");
                staff.DateEntry = DataHelper.GetDate(dr, "DateEntry");
                staff.Wage = DataHelper.GetDouble(dr, "Wage");
                staff.Note = DataHelper.Get(dr, "Note");
                staff.WorkingDay = DataHelper.GetInt(dr, "WorkingDay");
                staff.Authorities = DataHelper.GetInt(dr, "Authorities");
                list.Add(staff);
            }
            return list;
        }

        public static bool DeleteStaff(int staffID)
        {
            using (DBManager dbManager = new DBManager("stp_Get_Staff"))
            {
                try
                {
                    dbManager.Add("p_PK_StaffID", staffID);
                    dbManager.Add("IsDelete", 0);
                    dbManager.ExecuteNonQuery();
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }
        }

        public static bool SaveStaff(Staff staff, bool isUpdate)
        {
            GroupStaff gs = staff.GroupStaff;
            using (DBManager dbManager = new DBManager("stp_Create_Staff"))
            {
                try
                {
                    dbManager.Add("p_PK_StaffID", staff.PkStaffId);
                    dbManager.Add("p_FK_GroupStaffID", gs.PkGroupStaffId);
                    dbManager.Add("p_UserName", staff.UserName);
                    dbManager.Add("p_Password", staff.PassWord);
                    dbManager.Add("p_Name", staff.Name);
                    dbManager.Add("p_Address", staff.Address);
                    dbManager.Add("p_Phone", staff.Phone);
                    dbManager.Add("p_Email", staff.Email);
                    dbManager.Add("p_Passport", staff.Passport);
                    dbManager.Add("p_BirthDay", staff.BirthDay);
                    dbManager.Add("p_BirthPlace", staff.BirthPlace);
                    dbManager.Add("p_Sojourn", staff.Sojourn);
                    dbManager.Add("p_Resident", staff.Resident);
                    dbManager.Add("p_DateEntry", staff.DateEntry);
                    dbManager.Add("p_Wage", staff.Wage);
                    dbManager.Add("p_Note", staff.Note);
                    dbManager.Add("p_Authorities", staff.Authorities);
                    dbManager.Add("IsUpdate", isUpdate);
                    dbManager.Add("p_WorkingDay", staff.WorkingDay);
                    dbManager.ExecuteNonQuery();
                    return true;
                }
                catch (Exception)
                {

                    return false;
                }

            }
        }
    }
}